package day4;

import java.util.Scanner;

import org.junit.Test;

public class Demo {
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生的数量：");
		int count = scanner.nextInt();
		// int[] array = new int[count];
		Student[] students = new Student[count];
		for (int i = 1; i < students.length; i++) {
			System.out.println("请输入id：");
			int id = scanner.nextInt();
			System.out.println("请输入名字：");
			String name = scanner.next();
			System.out.println("请输入年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入班级名字：");
			String className = scanner.next();
			
			Student student = new Student();
			// 每次循环构造一个Student对象
			student.setId(id);
			student.setName(name);
			student.setAge(age);
			student.setClassName(className);
			// 把构造的学生对象放到指定下标位置
			students[i] = student;
		}
		
		System.out.println("-----------------------");
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
//			System.out.println(student.id);
//			System.out.println(student.name);
//			System.out.println(student.age);
//			System.out.println(student.className);
			System.out.println(student.toString());
			System.out.println(student);
		}
		System.out.println("-----------------------");
		
		while (true) {
			System.out.println("根据id查找输入：1");
			System.out.println("根据name查找输入：2");
			System.out.println("根据age查找输入：3");
			System.out.println("根据className查找输入：4");
			System.out.println("退出：0");
			int type = scanner.nextInt();
			if (type == 0) {
				System.out.println("退出搜索");
				break;
			}
			
			switch (type) {
			case 1: // 根据id查找
				System.out.println("请输入要查找的学生id：");
				int id = scanner.nextInt();
				// 根据id查找到了没有
				// boolean isExistId = false;
				boolean isSearchByIdFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					if (student.getId() == id) {
						isSearchByIdFind = true;
						System.out.println(student);
					}
				}
				// 如果遍历完了还是false，证明没有该信息学生
				if (isSearchByIdFind == false) {
					System.out.println("没有该信息的学生");
				}
				break;
			case 2: // 根据name查找
				System.out.println("请输入要查找的名字：");
				String name = scanner.next();
				boolean isSearchByNameFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					// 判断字符串的内容是不是相等，不能用==，要是用equals()方法
					if (name.equals(student.getName())) {
						isSearchByNameFind = true;
						System.out.println(student);
					}
				}
				if (isSearchByNameFind == false) {
					System.out.println("没有该信息的学生");
				}
				break;
			case 3: // 根据age查找
				System.out.println("请输入要查找的学生age：");
				int age = scanner.nextInt();
				// 根据age查找到了没有
				boolean isSearchByAgeFind = false;
				for (int i = 0; i < students.length; i++) {
					Student student = students[i];
					if (student.getAge() == age) {
						isSearchByAgeFind = true;
						System.out.println(student);
					}
				}
				// 如果遍历完了还是false，证明没有该信息学生
				if (isSearchByAgeFind == false) {
					System.out.println("没有该信息的学生");
				}
				break;
			case 4: // 根据classNmae查找
				System.out.println("4");
				break;
			default:
				break;
			}
			
		}
		
		
	}
	

	@Test
	public void test1() {
		int num = 3;
		int[] array = new int[4];
		String string = new String("abc");
		String str = "abc";
		// new出Student对象
		Student zhangsan = new Student();
		// The field Student.id is not visible
		zhangsan.setId(1);
		zhangsan.setName( "张三");
		zhangsan.setAge(23);
		zhangsan.setClassName("Java1912");
		zhangsan.study();
		System.out.println(zhangsan);
		System.out.println(zhangsan.toString());
		
		Student lisi = new Student();
		lisi.setId(2);
		lisi.setName("李四");;
		lisi.setAge(23);
		lisi.setClassName("UI1912");
		lisi.study();
		
		int i = 0220;
		System.out.println(i);
	}
	
	
	@Test
	public void test11() {
		// null可以赋值给所有的引用数据类型
		// 引用数据类型的默认值就是null
		// The local variable array may not have been initialized
		int[] array = null ;
		int num = array[0];// NullPointerException
		
		Student student = null;
		// null.id  NullPointerException
		System.out.println(student.getId());
	}
	
	@Test
	public void test2() {
		Student student = new Student();
		System.out.println(student.getId());// 0
		// student.null是不会报NullPointerException,只有null.属性或者null.方法
		// 如果是数组的话null[i]才会报NullPointerException
		System.out.println(student.getName());// null
		// System.out.println(student.array);// null
	}
	
	@Test
	public void test55() {
		Student student = new Student();
		// student.age = 200;
		student.setAge(200);
		System.out.println(student.getAge());
	}

}
